package com.cjx.stars.controller;

import com.cjx.stars.common.Response;
import com.cjx.stars.common.ResponseUtils;
import com.cjx.stars.constant.FailMessage;
import com.cjx.stars.pojo.request.TeamCoverUpdateRequest;
import com.cjx.stars.pojo.table.Team;
import com.cjx.stars.pojo.request.JoinTeam;
import com.cjx.stars.pojo.request.TeamKickUser;
import com.cjx.stars.pojo.request.TeamQuitRequest;
import com.cjx.stars.service.TeamService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.cjx.stars.utils.RegexValidationUtils.checkTeamStatus;

/**
 * @author cjx
 * @description 队伍相关接口
 * @createDate 2024/3/16 17:46
 **/
@RestController
@RequestMapping("/team")
@Slf4j
public class TeamController {

    @Resource
    private TeamService teamService;

    @PostMapping("/add")
    public Response<?> addTeam(@RequestBody Team team) {
        if (team == null){
            return ResponseUtils.fail(FailMessage.ERROR_PARAMETER_IS_BLACK);
        }
        return teamService.addTeam(team);
    }

    @GetMapping("/list")
    public Response<?> getTeamList(@RequestParam String searchText , @RequestParam int status ,@RequestParam int currentPage) {
        if (!checkTeamStatus(status)){
            return ResponseUtils.fail(FailMessage.ERROR_INCORRECT_FORMAT);
        }
        return teamService.getTeamList(searchText, status, currentPage);
    }

    @GetMapping("/list/my/join")
    public Response<?> myJoinTeam(@RequestParam String searchText  ,@RequestParam int currentPage) {
        return teamService.myJoinTeam(searchText,currentPage);
    }

    @GetMapping("/list/my/create")
    public Response<?> getMyTeamList(@RequestParam String searchText , @RequestParam int currentPage) {
        return teamService.getMyCreateTeamList(searchText,  currentPage);
    }

    @GetMapping("{teamId}")
    public Response<?> queryTeam(@PathVariable("teamId") Long teamId) {
        return teamService.queryTeam(teamId);
    }

    @PostMapping("/join")
    public Response<?> joinTeam(@RequestBody JoinTeam joinTeam) {
        if (joinTeam == null){
            return ResponseUtils.fail(FailMessage.ERROR_PARAMETER_IS_BLACK);
        }
        return teamService.joinTeam(joinTeam);
    }



    @PostMapping("/quit")
    public Response<?> quitTeam(@RequestBody TeamQuitRequest teamQuitRequest) {
        if (teamQuitRequest == null){
            return ResponseUtils.fail(FailMessage.ERROR_PARAMETER_IS_BLACK);
        }
        return teamService.quitTeam(teamQuitRequest.getTeamId());
    }

    @PostMapping("/delete")
    public Response<?> deleteTeam(@RequestBody TeamQuitRequest teamQuitRequest) {
        if (teamQuitRequest == null){
            return ResponseUtils.fail(FailMessage.ERROR_PARAMETER_IS_BLACK);
        }
        return teamService.deleteTeam(teamQuitRequest.getTeamId());
    }

    @GetMapping("/member/{teamId}")
    public Response<?> getMember(@PathVariable("teamId") Long teamId) {
        return teamService.getMember(teamId);
    }

    @PostMapping("/kick")
    public Response<?> kickUser(@RequestBody TeamKickUser teamKickUser) {
        if (teamKickUser == null){
            return ResponseUtils.fail(FailMessage.ERROR_PARAMETER_IS_BLACK);
        }
        Long userId = teamKickUser.getUserId();
        Long teamId = teamKickUser.getTeamId();
        if (userId == null || teamId == null){
            return ResponseUtils.fail(FailMessage.ERROR_PARAMETER_IS_BLACK);
        }
        return teamService.kickUser(teamId,userId);
    }

    @PostMapping("/update")
    public Response<?> teamUpdate(@RequestBody Team team) {
        if (team == null){
            return ResponseUtils.fail(FailMessage.ERROR_PARAMETER_IS_BLACK);
        }

        return teamService.teamUpdate(team);
    }

    @GetMapping("/list/my/join/all")
    public Response<?> myJoinTeams() {
        return teamService.myJoinTeams();
    }

    /**
     * 更新封面图片
     *
     * @param teamCoverUpdateRequest 更新封面图片请求
     * @return {@link Response}<{@link String}>
     */
    @PutMapping("/cover")
    @ApiOperation(value = "更新封面图片")
    @ApiImplicitParams({@ApiImplicitParam(name = "teamCoverUpdateRequest", value = "队伍封面更新请求"),
            @ApiImplicitParam(name = "request", value = "request请求")})
    public Response<String> changeCoverImage(TeamCoverUpdateRequest teamCoverUpdateRequest) {
        teamService.changeCoverImage(teamCoverUpdateRequest);
        return ResponseUtils.success();
    }


}
